<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>苏州旅行助手 - 测试页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            padding: 20px;
            background: #f0f9ff;
        }
        .test-container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .test-item {
            margin: 10px 0;
            padding: 10px;
            border: 1px solid #e5e7eb;
            border-radius: 4px;
        }
        .success { border-color: #10b981; background: #f0fdf4; }
        .error { border-color: #ef4444; background: #fef2f2; }
        button {
            background: #0ea5e9;
            color: white;
            border: none;
            padding: 8px 16px;
            border-radius: 4px;
            cursor: pointer;
            margin: 5px;
        }
    </style>
</head>
<body>
    <div class="test-container">
        <h1>🧪 苏州旅行助手 - 功能测试</h1>
        
        <div class="test-item" id="config-test">
            <h3>配置文件测试</h3>
            <p id="config-result">测试中...</p>
        </div>
        
        <div class="test-item" id="database-test">
            <h3>数据库测试</h3>
            <p id="database-result">测试中...</p>
        </div>
        
        <div class="test-item" id="api-test">
            <h3>API测试</h3>
            <p id="api-result">测试中...</p>
        </div>
        
        <div style="margin-top: 20px;">
            <button onclick="runAllTests()">🔄 重新测试</button>
            <button onclick="window.open('amap-version.html', '_blank')">🚀 打开主应用</button>
        </div>
    </div>

    <script src="config.js"></script>
    <script src="database.js"></script>
    
    <script>
        async function testConfig() {
            const configDiv = document.getElementById('config-test');
            const resultDiv = document.getElementById('config-result');
            
            try {
                if (typeof window.AppConfig === 'undefined') {
                    throw new Error('配置文件未加载');
                }
                
                const config = window.AppConfig;
                if (!config.amap || !config.amap.key) {
                    throw new Error('高德地图配置缺失');
                }
                
                configDiv.className = 'test-item success';
                resultDiv.innerHTML = '✅ 配置文件加载成功<br>高德地图Key: ' + config.amap.key.substring(0, 8) + '...';
                return true;
            } catch (error) {
                configDiv.className = 'test-item error';
                resultDiv.innerHTML = '❌ 配置文件加载失败: ' + error.message;
                return false;
            }
        }
        
        async function testDatabase() {
            const databaseDiv = document.getElementById('database-test');
            const resultDiv = document.getElementById('database-result');
            
            try {
                if (typeof window.dbManager === 'undefined') {
                    throw new Error('数据库管理器未加载');
                }
                
                await window.dbManager.init();
                
                // 测试保存和获取数据
                const testRoute = {
                    name: '测试路线',
                    start: { name: '起点', coordinates: [120.0, 31.0] },
                    end: { name: '终点', coordinates: [121.0, 32.0] },
                    totalDistance: 1000,
                    totalDuration: 600,
                    totalCost: 0,
                    isActive: true
                };
                
                const routeId = await window.dbManager.saveRoute(testRoute);
                const routes = await window.dbManager.getRoutes();
                
                if (routes.length > 0) {
                    databaseDiv.className = 'test-item success';
                    resultDiv.innerHTML = '✅ 数据库初始化成功<br>已保存 ' + routes.length + ' 条路线';
                    return true;
                } else {
                    throw new Error('数据保存失败');
                }
            } catch (error) {
                databaseDiv.className = 'test-item error';
                resultDiv.innerHTML = '❌ 数据库初始化失败: ' + error.message;
                return false;
            }
        }
        
        async function testAPI() {
            const apiDiv = document.getElementById('api-test');
            const resultDiv = document.getElementById('api-result');
            
            try {
                const config = window.AppConfig;
                const weatherUrl = `${config.amap.baseUrl}/v3/weather/weatherInfo?key=${config.amap.key}&city=${config.api.weather.city}&extensions=${config.api.weather.extensions}`;
                
                const response = await fetch(weatherUrl);
                const data = await response.json();
                
                if (data.status === '1') {
                    apiDiv.className = 'test-item success';
                    resultDiv.innerHTML = '✅ API连接成功<br>天气: ' + data.lives[0].weather + ' ' + data.lives[0].temperature + '°C';
                    return true;
                } else {
                    throw new Error('API返回错误: ' + data.info);
                }
            } catch (error) {
                apiDiv.className = 'test-item error';
                resultDiv.innerHTML = '❌ API连接失败: ' + error.message;
                return false;
            }
        }
        
        async function runAllTests() {
            console.log('开始运行所有测试...');
            
            const configOk = await testConfig();
            const databaseOk = await testDatabase();
            const apiOk = await testAPI();
            
            if (configOk && databaseOk && apiOk) {
                console.log('✅ 所有测试通过！');
            } else {
                console.log('❌ 部分测试失败');
            }
        }
        
        // 页面加载时自动运行测试
        window.addEventListener('load', runAllTests);
    </script>
</body>
</html>
